<?php

namespace app\controllers;

use app\models\MedicalTests;
use lithium\security\Auth;
use lithium\action\DispatchException;

class MedicalTestsController extends \lithium\action\Controller {

	public function index() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$medicalTests = MedicalTests::all(array(
			'order' => array('Name' => 'ASC')
		));
		return compact('medicalTests');
	}

	public function view() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$medicalTest = MedicalTests::first($this->request->id);
		return compact('medicalTest');
	}

	public function add() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$medicalTest = MedicalTests::create();

		if (($this->request->data) && $medicalTest->save($this->request->data)) {
			return $this->redirect(array('MedicalTests::view', 'args' => array($medicalTest->id)));
		}
		return compact('medicalTest');
	}

	public function edit() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$medicalTest = MedicalTests::find($this->request->id);

		if (!$medicalTest) {
			return $this->redirect('MedicalTests::index');
		}
		if (($this->request->data) && $medicalTest->save($this->request->data)) {
			return $this->redirect(array('MedicalTests::view', 'args' => array($medicalTest->id)));
		}
		return compact('medicalTest');
	}

	public function delete() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		if (!$this->request->is('post') && !$this->request->is('delete')) {
			$msg = "MedicalTests::delete can only be called with http:post or http:delete.";
			throw new DispatchException($msg);
		}
		MedicalTests::find($this->request->id)->delete();
		return $this->redirect('MedicalTests::index');
	}
}

?>